import 'package:get/get.dart';
import '../models/chat_message.dart';
import '../models/chat_model.dart';
import 'chat_list_controller.dart';

class ChatDetailController extends GetxController {
  final String chatId;
  final RxList<ChatMessage> messages = <ChatMessage>[].obs;
  late ChatModel chat;

  ChatDetailController(this.chatId);

  @override
  void onInit() {
    super.onInit();
    // 获取聊天信息
    final ChatListController chatListController =
        Get.find<ChatListController>();
    chat = chatListController.chatList.firstWhere((chat) => chat.id == chatId);

    // 加载模拟消息数据
    loadMockMessages();
  }

  // 加载模拟消息数据
  void loadMockMessages() {
    // 清空消息列表
    messages.clear();

    // 添加模拟消息
    if (chat.name == '【财务】前后端打包群(23)') {
      messages.addAll([
        ChatMessage(
          id: '1',
          text: '@赖丽娜 核算和凭证好像都进不去了',
          senderName: '柯铭佳',
          senderAvatar: 'assets/images/avatar1.png',
          isSender: false,
          timestamp: DateTime.now().subtract(const Duration(minutes: 30)),
        ),
        ChatMessage(
          id: '2',
          text: '看看js的404错误',
          senderName: '柯铭佳',
          senderAvatar: 'assets/images/avatar1.png',
          isSender: false,
          timestamp: DateTime.now().subtract(const Duration(minutes: 25)),
        ),
        ChatMessage(
          id: '3',
          text: '再部署',
          senderName: '赖丽娜',
          senderAvatar: 'assets/images/avatar2.png',
          isSender: false,
          timestamp: DateTime.now().subtract(const Duration(minutes: 20)),
        ),
        ChatMessage(
          id: '4',
          text: '稍等',
          senderName: '赖丽娜',
          senderAvatar: 'assets/images/avatar2.png',
          isSender: false,
          timestamp: DateTime.now().subtract(const Duration(minutes: 15)),
        ),
        ChatMessage(
          id: '5',
          text: '现在就部署了一台机器',
          senderName: '谢冰杰(财务-测试)',
          senderAvatar: 'assets/images/avatar3.png',
          isSender: false,
          timestamp: DateTime.now().subtract(const Duration(minutes: 10)),
        ),
        ChatMessage(
          id: '6',
          text: '刷新几次 有时可以有时不行',
          senderName: '柯铭佳',
          senderAvatar: 'assets/images/avatar1.png',
          isSender: false,
          timestamp: DateTime.now().subtract(const Duration(minutes: 5)),
        ),
      ]);
    } else {
      // 为其他聊天添加一些默认消息
      messages.addAll([
        ChatMessage(
          id: '1',
          text: '你好，这是一条测试消息',
          senderName: chat.name,
          senderAvatar: chat.avatar,
          isSender: false,
          timestamp: DateTime.now().subtract(const Duration(minutes: 30)),
        ),
        ChatMessage(
          id: '2',
          text: '你好，我收到了你的消息',
          senderName: '我',
          isSender: true,
          timestamp: DateTime.now().subtract(const Duration(minutes: 25)),
        ),
        ChatMessage(
          id: '3',
          text: chat.lastMessage,
          senderName: chat.name,
          senderAvatar: chat.avatar,
          isSender: false,
          timestamp: DateTime.now().subtract(const Duration(minutes: 5)),
        ),
      ]);
    }
  }

  // 发送消息
  void sendMessage(String text) {
    if (text.isEmpty) return;

    final message = ChatMessage(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      text: text,
      senderName: '我',
      isSender: true,
      timestamp: DateTime.now(),
    );

    messages.add(message);
  }
}
